<template>
  <div class="container">
    <editPageNew
      ref="editPage"
      :rightBtnArr="rightBtnArr"
      :creatInfo="creatInfo"
      showTopTitle
    >
      <template slot="editPageTitle">
        <i class="TitleImg"></i>
        <span class="title"> 工作票变更申请 {{ changeId }}</span>
      </template>
      <dialogPublic
        class="submitDialog"
        title="操作提示"
        :show.sync="showDialogSubmit"
        @doSubmit="dialogSubmit"
        @doClose="() => (showDialogSubmit = false)"
        width="300px"
      >
        <span>确定要提交吗？</span>
      </dialogPublic>
      <dialogPublic
        title="工作票"
        :show.sync="showDialogWork"
        @doSubmit="dialogSubmitWork"
        @doClose="() => (showDialogWork = false)"
        fullscreen
      >
        <workTicket ref="workOrderTable" />
      </dialogPublic>
      <sign :showTemporary="showTemporary" @doSign="doSign" ref="sign"></sign>
      <el-collapse v-model="activeNames">
        <el-collapse-item class="basic" name="0">
          <template slot="title"> 基本信息 </template>
          <editForm
            :formConfig="basicConfig"
            ref="basicForm"
            :formData="basicFormData"
          >
            <template #workTicketCode="{ editFormInfo }">
              <el-input
                placeholder="请选择工作票票号"
                v-model="editFormInfo.workTicketCode"
                size="mini"
                class="input-link"
                :readonly="true"
                @click.native="
                  goWorkTicket(
                    editFormInfo.workTicketId,
                    editFormInfo.workTicketType
                  )
                "
              >
                <el-button
                  v-if="!workOrderDisable"
                  slot="append"
                  @click.stop="getTicketNum"
                  :disabled="workOrderDisable"
                  >选择</el-button
                >
              </el-input>
            </template>
          </editForm>
        </el-collapse-item>
        <el-collapse-item name="1" v-show="type == 1 && changeShow">
          <template slot="title"> 变更内容 </template>
          <editForm
            :formConfig="changeConfig1"
            ref="changeForm1"
            :formData="changeFormData1"
          ></editForm>
        </el-collapse-item>
        <el-collapse-item name="2" v-show="type == 2 && changeShow">
          <template slot="title"> 变更内容 </template>
          <editForm
            :formConfig="changeConfig2"
            ref="changeForm2"
            :formData="changeFormData2"
          ></editForm>
        </el-collapse-item>
        <el-collapse-item name="3" v-show="type == 3 && changeShow">
          <template slot="title"> 变更内容 </template>
          <editForm
            :formConfig="changeConfig3"
            ref="changeForm3"
            :formData="changeFormData3"
          ></editForm>
        </el-collapse-item>
        <el-collapse-item name="4" v-show="type == 4 && changeShow">
          <template slot="title"> 变更内容 </template>
          <editForm
            :formConfig="changeConfig4"
            ref="changeForm4"
            :formData="changeFormData4"
          ></editForm>
        </el-collapse-item>
        <el-collapse-item name="5" v-show="type == 5 && changeShow">
          <template slot="title"> 变更内容 </template>
          <editForm
            :formConfig="changeConfig5"
            ref="changeForm5"
            :formData="changeFormData5"
          ></editForm>
        </el-collapse-item>
        <el-collapse-item name="6" v-show="type == 6 && changeShow">
          <template slot="title"> 变更内容 </template>
          <editForm
            :formConfig="changeConfig6"
            ref="changeForm6"
            :formData="changeFormData6"
          ></editForm>
        </el-collapse-item>
      </el-collapse>
    </editPageNew>
  </div>
</template>
<script>
import workTicket from "../workTicketProcessing/workTicket";
import sign from "../workTicketProcessing/process/sign";

import {
  getById,
  insertData,
  insertManagerData,
  insertDelayData,
  insertIntermittentData,
  updateIntermittentData,
  insertContentData,
  insertMemberData,
  insertSafeData,
  getWorkTicketData,
  getByTicket,
} from "@/api/twoTicketManagement/workTicket.js";
export default {
  components: {
    workTicket,
    sign,
  },
  data() {
    return {
      updateTime: "",
      mustSignAccount: "", // 需要签名的工作负责人账户
      mustSignName: "", // 需要签名的工作负责人姓名
      showTemporary: true,
      creatInfo: {},
      oldMemberName: "",
      oldMemberAccount: "",
      memberName: [],
      memberAccount: [],
      changeShow: true,
      showDialogSubmit: false, // 提交弹框
      showDialogWork: false, // 工作票票号
      workOrderDisable: false, // 选择工作票票号
      changeId: "", // 变更票号
      type: "", // 变更类型
      formData: "",
      activeNames: ["0"],
      memberOption: [],
      det: [],
      // 基本信息表单
      basicConfig: {
        labelWidth: "120px",
        itemArr: [
          {
            label: "工作票票号",
            prop: "workTicketCode",
            type: "select",
            span: 6,
            trigger: "change",
            required: true,
            selectOption: []
          },
          {
            label: "工作票类型",
            prop: "workTicketType$",
            type: "text",
            span: 6,
            trigger: "change",
            required: true,
            disabled: true,
          },
          {
            label: "变更类型",
            prop: "type$",
            type: "text",
            span: 6,
            trigger: "change",
            required: true,
            disabled: true,
          },
          {
            label: "变更申请人",
            prop: "directorName",
            idsStr: "director",
            type: "text",
            span: 6,
            trigger: "change",
            required: true,
            disabled: true,
          },
        ],
      },
      // 基本信息表单数据
      basicFormData: {
        directorName: this.$store.state.user.userInfo.name,
        deptId: this.$store.state.user.userInfo.deptId,
        deptName: this.$store.state.user.userInfo.deptName,
        changeDate: this.$utils.getCurrentDate("minutes"),
      },
      // 变更内容表单-工作负责人变动
      changeConfig1: {
        labelWidth: "120px",
        itemArr: [
          {
            label: "原工作负责人",
            prop: "formerDirectorName",
            type: "text",
            span: 6,
            required: true,
            disabled: true,
          },
          {
            label: "新工作负责人（签名）",
            prop: "newDirectorName",
            type: "text",
            placeholder: "点击签名验证",
            span: 6,
            required: true,
            focusMethod: (item, res) => {
              this.showTemporary = true;
              let position = "";
              switch (this.basicFormData.workTicketType) {
                case "ElectricalFirst":
                  position = "机电工作负责人";
                  break;
                case "ElectricalSecond":
                  position = "机电工作负责人";
                  break;
                case "ThirdTicket":
                  position = "机电工作负责人（第三种）";
                  break;
                case "MechanicalTicket":
                  position = "机电工作负责人";
                  break;
                case "PowerLineFirst":
                  position = "电力线路工作负责人";
                  break;
                case "PowerLineSecond":
                  position = "电力线路工作负责人";
                  break;
                case "FirstLevelHotWork":
                  position = "动火工作票负责人";
                  break;
                case "SecondLevelHotWork":
                  position = "动火工作票负责人";
                  break;
                case "HydraulicWorkFist":
                  position = "任何人";
                  break;
                case "HydraulicWorkSecond":
                  position = "任何人";
                  break;
                case "YcPowerLineSecond":
                  position = "任何人";
                  break;
                case "YcPowerLineFirst":
                  position = "任何人";
                  break;
                default: 
                  position = "任何人";
              }
              let obj = {
                formData: "changeForm1",
                account: "newDirectorAccount",
                phone: "newDirectorPhone",
                name: "newDirectorName",
                disAccount: res.permitAccount || "",
                mustAccount: "",
                position: position,
              };
              this.$refs.sign.openSign(obj);
            },
          },
          {
            label: "工作许可人（签名）",
            prop: "permitName",
            type: "text",
            placeholder: "点击签名验证",
            span: 6,
            required: true,
            focusMethod: (item, res) => {
              this.showTemporary = false;
              let obj = {
                formData: "changeForm1",
                account: "permitAccount",
                phone: "permitPhone",
                name: "permitName",
                disAccount: res.newDirectorAccount || "",
                mustAccount: "",
                position: "工作许可人",
              };
              this.$refs.sign.openSign(obj);
            },
          },
          {
            label: "工作票签发人（签名）",
            prop: "singName",
            type: "text",
            placeholder: "点击签名验证",
            span: 6,
            required: true,
            focusMethod: () => {
              this.showTemporary = false;
              let position = "";
              switch (this.basicFormData.workTicketType) {
                case "ThirdTicket":
                  this.showTemporary = true;
                  position = "任何人";
                  break;
                default: 
                  this.showTemporary = true;
                  position = "任何人";
              }
              let obj = {
                formData: "changeForm1",
                account: "singAccount",
                phone: "singPhone",
                name: "singName",
                disAccount: "",
                mustAccount: "",
                position: position,
              };
              this.$refs.sign.openSign(obj);
            },
          },
          {
            label: "交接时间",
            prop: "changeTime",
            type: "datetime",
            span: 6,
            required: true,
          },
        ],
      },
      // 变更内容表单数据-工作负责人变动
      changeFormData1: {},
      // 变更内容表单-工作票延期
      changeConfig2: {
        labelWidth: "120px",
        itemArr: [
          {
            label: "有效期延长至",
            prop: "delayTime",
            type: "datetime",
            trigger: "change",
            span: 6,
            required: true,
            methods: (item, res) => {
              if (res.estimatedEndTime) {
                let endTime = new Date(item); ///选择的时间
                let startTime = new Date(res.estimatedEndTime); //时间下限
                if (endTime.getTime() <= startTime.getTime()) {
                  this.$message.error("有效期延长时间应大于计划结束时间");
                  this.changeFormData2 = Object.assign(res, { delayTime: "" });
                  return false;
                }
              }
            },
          },
          {
            label: "工作负责人（签名）",
            prop: "directorName",
            type: "text",
            placeholder: "点击签名验证",
            span: 6,
            required: true,
            focusMethod: () => {
              this.showTemporary = true;
              let obj = {
                formData: "changeForm2",
                account: "directorAccount",
                phone: "directorPhone",
                name: "directorName",
                disAccount: "",
                mustAccount: this.mustSignAccount,
                position: "任何人",
              };
              this.$refs.sign.openSign(obj);
            },
          },
          {
            label: "值班负责人（签名）",
            prop: "dutyPersonName",
            type: "text",
            placeholder: "点击签名验证",
            span: 6,
            required: true,
            focusMethod: () => {
              this.showTemporary = false;
              let obj = {
                formData: "changeForm2",
                account: "dutyPersonAccount",
                phone: "dutyPersonPhone",
                name: "dutyPersonName",
                disAccount: "",
                mustAccount: "",
                position: "值班负责人",
              };
              this.$refs.sign.openSign(obj);
            },
          },
        ],
      },
      // 变更内容表单数据-工作票延期
      changeFormData2: {},
      // 变更内容表单-工作间断
      changeConfig3: {
        labelWidth: "120px",
        itemArr: [
          {
            label: "工作间断时间",
            prop: "interruptTime",
            type: "datetime",
            span: 8,
            required: true,
          },
          {
            label: "工作负责人（签名）",
            prop: "directorNameInterrupt",
            type: "text",
            placeholder: "点击签名验证",
            span: 8,
            required: true,
            focusMethod: () => {
              this.showTemporary = true;
              let obj = {
                formData: "changeForm3",
                account: "directorAccountInterrupt",
                phone: "directorPhoneInterrupt",
                name: "directorNameInterrupt",
                disAccount: "",
                mustAccount: this.mustSignAccount,
                position: "任何人",
              };
              this.$refs.sign.openSign(obj);
            },
          },
          {
            label: "工作许可人（签名）",
            prop: "permitNameInterrupt",
            type: "text",
            placeholder: "点击签名验证",
            span: 8,
            required: true,
            focusMethod: () => {
              this.showTemporary = false;
              let obj = {
                formData: "changeForm3",
                account: "permitAccountInterrupt",
                phone: "permitPhoneInterrupt",
                name: "permitNameInterrupt",
                disAccount: "",
                mustAccount: "",
                position: "工作许可人",
              };
              this.$refs.sign.openSign(obj);
            },
          },
          {
            label: "工作开始时间",
            prop: "startTime",
            type: "datetime",
            span: 8,
            required: true,
            isHide: true,
          },
          {
            label: "开始工作许可人（签名）",
            prop: "permitNameStart",
            type: "text",
            placeholder: "点击签名验证",
            span: 8,
            required: true,
            isHide: true,
            focusMethod: () => {
              this.showTemporary = false;
              let obj = {
                formData: "changeForm3",
                account: "permitAccountStart",
                phone: "permitPhoneStart",
                name: "permitNameStart",
                disAccount: "",
                mustAccount: "",
                position: "工作许可人",
              };
              this.$refs.sign.openSign(obj);
            },
          },
          {
            label: "工作负责人（签名）",
            prop: "directorNameStart",
            type: "text",
            placeholder: "点击签名验证",
            span: 8,
            required: true,
            isHide: true,
            focusMethod: () => {
              this.showTemporary = true;
              let obj = {
                formData: "changeForm3",
                account: "directorAccountStart",
                phone: "directorPhoneStart",
                name: "directorNameStart",
                disAccount: "",
                mustAccount: this.mustSignAccount,
                position: "任何人",
              };
              this.$refs.sign.openSign(obj);
            },
          },
        ],
      },
      // 变更内容表单数据-工作间断
      changeFormData3: {},
      // 变更内容表单-增加工作内容
      changeConfig4: {
        labelWidth: "120px",
        itemArr: [
          {
            label: "不变更安全措施下增加的工作内容",
            prop: "content",
            type: "textarea",
            autosize: true,
            span: 24,
            maxlength: 3000,
            required: true,
          },
          {
            label: "工作负责人（签名）",
            prop: "directorName",
            type: "text",
            placeholder: "点击签名验证",
            span: 8,
            required: true,
            focusMethod: () => {
              this.showTemporary = true;
              let obj = {
                formData: "changeForm4",
                account: "directorAccount",
                phone: "directorPhone",
                name: "directorName",
                disAccount: "",
                mustAccount: this.mustSignAccount,
                position: "任何人",
              };
              this.$refs.sign.openSign(obj);
            },
          },
          {
            label: "工作许可人（签名）",
            prop: "permitName",
            type: "text",
            placeholder: "点击签名验证",
            span: 8,
            required: true,
            focusMethod: () => {
              this.showTemporary = false;
              let obj = {
                formData: "changeForm4",
                account: "permitAccount",
                phone: "permitPhone",
                name: "permitName",
                disAccount: "",
                mustAccount: "",
                position: "工作许可人",
              };
              this.$refs.sign.openSign(obj);
            },
          },
        ],
      },
      // 变更内容表单数据-增加工作内容
      changeFormData4: {},
      // 变更内容表单-工作班成员变动
      changeConfig5: {
        labelWidth: "120px",
        itemArr: [
          {
            label: "变更时间",
            prop: "changeTime",
            type: "datetime",
            trigger: "change",
            span: 6,
            required: true,
          },
          {
            label: "工作班成员",
            prop: "memberName", //多选时，字段要为数组类型
            propArr: "memberNameArr",
            idsStr: "memberAccount",
            orgStr: "memberOrgStr",
            orgNameStr: "memberOrgName",
            orgIdStr: "memberOrgId",
            multiple: true, //是否多选
            isOutOrg: "", //是否是外单位;0:不是外单位；1是外单位；不做限制就不传此参数
            allowCreate: true, //是否允许创建条目
            option: [], //当前选中的项
            type: "user-select", //类型
            trigger: "change",
            rows: 4,
            methods: async (obj) => {
              this.det = [];
              this.memberName = [];
              this.memberAccount = [];
              obj.forEach((item) => {
                this.memberName.push(item.label);
                this.memberAccount.push(item.id);
                let obj = {
                  chanageId: "",
                  createTime: this.$utils.getCurrentDate("minutes"),
                  id: "",
                  memberAccount: item.id,
                  memberName: item.label,
                  memberUnit: item.orgId,
                  memberUnitName: item.orgName,
                  type: item.isOutOrg,
                };
                this.det.push(obj);
              });
              let oldName = this.oldMemberName.split(",");
              let newName = this.memberName;
              let sameName = oldName.filter((item) => newName.includes(item));
              let addMemberName = newName.filter(
                (item) => !sameName.includes(item)
              );
              let decreaseMemberName = oldName.filter(
                (item) => !sameName.includes(item)
              );
              this.$refs.changeForm5.setFormItem(
                "addMemberName",
                addMemberName.toString()
              );
              this.$refs.changeForm5.setFormItem(
                "decreaseMemberName",
                decreaseMemberName.toString()
              );
              let oldAccount = this.oldMemberAccount.split(",");
              let newAccount = this.memberAccount;
              let sameAccount = oldAccount.filter((item) =>
                newAccount.includes(item)
              );
              let addMemberAccount = newAccount.filter(
                (item) => !sameAccount.includes(item)
              );
              let decreaseMemberAccount = oldAccount.filter(
                (item) => !sameAccount.includes(item)
              );
              this.$refs.changeForm5.setFormItem(
                "addMemberAccount",
                addMemberAccount.toString()
              );
              this.$refs.changeForm5.setFormItem(
                "decreaseMemberAccount",
                decreaseMemberAccount.toString()
              );
            },
            required: true,
            span: 24,
          },
          {
            label: "工作负责人（签名）",
            prop: "directorName",
            type: "text",
            placeholder: "点击签名验证",
            span: 7,
            required: true,
            focusMethod: (item, res) => {
              this.showTemporary = true;
              let obj = {
                formData: "changeForm5",
                account: "directorAccount",
                phone: "directorPhone",
                name: "directorName",
                disAccount: res.permitAccount || "",
                mustAccount: this.mustSignAccount,
                position: "任何人",
              };
              this.$refs.sign.openSign(obj);
            },
          },
          {
            label: "工作许可人（签名）",
            prop: "permitName",
            type: "text",
            placeholder: "点击签名验证",
            span: 7,
            required: true,
            focusMethod: (item, res) => {
              this.showTemporary = false;
              let obj = {
                formData: "changeForm5",
                account: "permitAccount",
                phone: "permitPhone",
                name: "permitName",
                disAccount: res.directorAccount || "",
                mustAccount: "",
                position: "工作许可人",
              };
              this.$refs.sign.openSign(obj);
            },
          },
          {
            label: "增加的工作班成员",
            prop: "addMemberName",
            type: "textarea",
            autosize: true,
            span: 12,
            disabled: true,
          },
          {
            label: "减少的工作班成员",
            prop: "decreaseMemberName",
            type: "textarea",
            autosize: true,
            span: 12,
            disabled: true,
          },
        ],
      },
      // 变更内容表单数据-工作班成员变动
      changeFormData5: {},
      // 变更内容表单-部分安全措施变动
      changeConfig6: {
        labelWidth: "120px",
        itemArr: [
          {
            label: "工作负责人（签名）",
            prop: "directorName",
            type: "text",
            placeholder: "点击签名验证",
            span: 6,
            required: true,
            focusMethod: () => {
              this.showTemporary = true;
              let obj = {
                formData: "changeForm6",
                account: "directorAccount",
                phone: "directorPhone",
                name: "directorName",
                disAccount: "",
                mustAccount: this.mustSignAccount,
                position: "任何人",
              };
              this.$refs.sign.openSign(obj);
            },
          },
          {
            label: "工作许可人（签名）",
            prop: "permitName",
            type: "text",
            placeholder: "点击签名验证",
            span: 6,
            required: true,
            focusMethod: () => {
              this.showTemporary = false;
              let obj = {
                formData: "changeForm6",
                account: "permitAccount",
                phone: "permitPhone",
                name: "permitName",
                disAccount: "",
                mustAccount: "",
                position: "工作许可人",
              };
              this.$refs.sign.openSign(obj);
            },
          },
          {
            label: "变更时间",
            prop: "changeTime",
            type: "datetime",
            span: 6,
            required: true,
          },
          {
            label: "部分安全措施变动",
            prop: "securityArrangement",
            type: "textarea",
            autosize: true,
            span: 24,
            maxlength: 500,
            required: true,
          },
        ],
      },
      // 变更内容表单数据-部分安全措施变动
      changeFormData6: {},
    };
  },
  computed: {
    // 操作按钮
    rightBtnArr() {
      let arr = [
        {
          text: "提交",
          icon: "icon-tijiao",
          disabled: !this.$store.getters.isBtnAuth("submit"),
          content: "未授权的功能",
          hide: this.pageType == "details",
          methods: () => {
            this.doSubmit();
          },
        },
      ];
      return arr;
    },
  },
  created() {
    this.init();
  },
  mounted() {},
  methods: {
    // 初始化信息
    init() {
      this.updateTime = this.$utils.getCurrentDate();
      this.pageType = this.$route.query.pageType;
      this.formData = JSON.parse(this.$route.query.formData);
      if (this.pageType == "add") {
        // 新增
        this.changeId = "变更票号";
        this.changeShow = false;
        this.$set(this.basicFormData, "type$", this.formData.type$);
        this.$set(this.basicFormData, "type", this.formData.type);
      } else if (this.pageType == "details") {
        // 详情
        this.$set(this.basicFormData, "type", this.formData.type);
        let types = this.$store.getters["dirtData"]("ticket_change_type");
        let obj = types.find((i) => i.value == this.basicFormData.type);
        let type$ = obj ? obj.label : this.formData.type;
        this.$set(this.formData, "type$", type$);
        this.$set(this.basicFormData, "type$", type$);
        this.getDetails(this.formData.id);
        this.workOrderDisable = true;
      } else if (this.pageType == "changeTicket") {
        this.changeId = "变更票号";
        this.workOrderDisable = true;
        this.$set(this.basicFormData, "type$", this.formData.type$);
        this.$set(this.basicFormData, "type", this.formData.type);
        this.getInfo(this.formData.id, this.formData.ticketType, 1);
      }
      let type = this.basicFormData.type$;
      switch (type) {
        case "工作负责人变更":
          this.type = 1;
          if (this.pageType == "details") {
            this.changeConfig1.itemArr.map((i) => (i.disabled = true));
          }
          break;
        case "工作票延期":
          let appCode = this.$store.getters.currentAppInfo.appCode;
          if (appCode == "EAM_YCSC") {
            this.changeConfig2.itemArr[2] = {
              label: "工作许可人（签名）",
              prop: "dutyPersonName",
              type: "text",
              placeholder: "点击签名验证",
              span: 6,
              required: true,
              focusMethod: () => {
                this.showTemporary = false;
                let obj = {
                  formData: "changeForm2",
                  account: "dutyPersonAccount",
                  phone: "dutyPersonPhone",
                  name: "dutyPersonName",
                  disAccount: "",
                  mustAccount: "",
                  position: "工作许可人",
                };
                this.$refs.sign.openSign(obj);
              },
            };
          }
          this.type = 2;
          if (this.pageType == "details") {
            this.changeConfig2.itemArr.map((i) => (i.disabled = true));
          }
          break;
        case "工作间断":
          this.type = 3;
          break;
        case "增加工作内容":
          this.type = 4;
          if (this.pageType == "details") {
            this.changeConfig4.itemArr.map((i) => (i.disabled = true));
          }
          break;
        case "工作班成员变动":
          this.type = 5;
          if (this.pageType == "details") {
            this.changeConfig5.itemArr.map((i) => (i.disabled = true));
          }
          break;
        case "部分安全措施变动":
          this.type = 6;
          if (this.pageType == "details") {
            this.changeConfig6.itemArr.map((i) => (i.disabled = true));
          }
          break;
      }
      this.activeNames.push(this.type.toString());
    },
    // 根据工作票id和类型获取信息
    async getInfo(id, type, flag) {
      let val = { id, type };
      let { data } = await getWorkTicketData(val);
      if (
        data.ticketChange &&
        data.ticketChange.ticketChangeDirectorList.length
      ) {
        this.mustSignAccount =
          data.ticketChange.ticketChangeDirectorList[
            data.ticketChange.ticketChangeDirectorList.length - 1
          ].newDirectorAccount;
        this.mustSignName =
          data.ticketChange.ticketChangeDirectorList[
            data.ticketChange.ticketChangeDirectorList.length - 1
          ].newDirectorName;
      } else {
        this.mustSignAccount = data.workTicket ? data.workTicket.director : data.director;
        this.mustSignName = data.workTicket ? data.workTicket.directorName : data.directorName;
      }
      if (flag) {
        let time = this.$utils.getCurrentDate("minutes");
        this.$set(this.changeFormData5, "changeTime", time);
        this.$set(this.changeFormData6, "changeTime", time);
        let type = data.workTicket ? data.workTicket.type : data.type;
        this.$set(this.basicFormData, "workTicketType", type);
        let types = this.$store.getters["dirtData"]("ticket_work_type");
        let obj = types.find((i) => i.value == type);
        this.$set(
          this.basicFormData,
          "workTicketType$",
          obj.label ? obj.label : type
        );
        this.basicFormData = Object.assign(
          {},
          {
            type: type,
            workTicketCode: data.workTicket ? data.workTicket.code : data.code,
            workTicketId: data.workTicket ? data.workTicket.id : data.id,
          },
          this.basicFormData
        );
      }
      switch (this.formData.type$) {
        case "工作负责人变更":
          this.$set(
            this.changeFormData1,
            "formerDirectorName",
            this.mustSignName
          );
          this.$set(
            this.changeFormData1,
            "formerDirectorAccount",
            this.mustSignAccount
          );
          break;
        case "工作票延期":
          this.$set(
            this.changeFormData2,
            "estimatedEndTime",
            data.workTicket
              ? data.workTicket.estimatedEndTime
              : data.estimatedEndTime
          );
          break;
        case "工作间断":
          break;
        case "增加工作内容":
          break;
        case "工作班成员变动":
          this.memberOption = [];
          setTimeout(() => {
            this.$refs.changeForm5.setItemOption(
              "memberName",
              this.memberOption
            );
          }, 0);
          let workMemberList = [];
          workMemberList = data.workMemberList;
          await getByTicket(id).then((res) => {
            if (res.code == 200) {
              if (res.data) {
                workMemberList = res.data.det;
              }
            }
          });
          this.$set(
            this.changeFormData5,
            "estimatedEndTime",
            data.workTicket
              ? data.workTicket.estimatedEndTime
              : data.estimatedEndTime
          );
          if (workMemberList.length) {
            const classInfo = {
              memberName: [],
              memberNameArr: [],
              memberAccount: [],
              memberOrgName: [],
              memberOrgId: [],
              memberOrgStr: [],
            };
            workMemberList.forEach((item) => {
              classInfo.memberName.push(item.memberName);
              classInfo.memberOrgStr.push(item.type);
              if (item.memberUnit) {
                classInfo.memberOrgName.push(item.memberUnitName);
                classInfo.memberOrgId.push(item.memberUnit);
                classInfo.memberAccount.push(item.memberAccount);
                classInfo.memberNameArr.push(
                  `${item.memberAccount}${item.memberUnit}`
                );
              } else {
                classInfo.memberAccount.push(item.memberAccount);
                classInfo.memberOrgName.push("");
                classInfo.memberOrgId.push("");
                classInfo.memberNameArr.push(item.memberName);
              }
              return;
            });
            this.changeFormData5.memberName = classInfo.memberName.join(",");
            this.changeFormData5.memberAccount =
              classInfo.memberAccount.join(",");
            this.changeFormData5.memberOrgName =
              classInfo.memberOrgName.join(",");
            this.changeFormData5.memberOrgId = classInfo.memberOrgId.join(",");
            this.changeFormData5.memberOrgStr =
              classInfo.memberOrgStr.join(",");
            this.memberOption = this.getOptionData(
              classInfo.memberName,
              classInfo.memberAccount,
              classInfo.memberOrgStr,
              classInfo.memberOrgName,
              classInfo.memberOrgId
            );
            setTimeout(() => {
              this.$refs.changeForm5.setItemOption(
                "memberName",
                this.memberOption
              );
              this.$refs.changeForm5.setFormItem(
                "memberNameArr",
                classInfo.memberNameArr
              );
            }, 0);
            this.det = [];
            this.changeFormData5 = Object.assign(
              {},
              this.changeFormData5,
              classInfo
            );
          }
          this.oldMemberName = "";
          this.memberName = [];
          this.memberAccount = [];
          for (let i = 0; i < workMemberList.length; i++) {
            this.memberName.push(workMemberList[i].memberName),
              this.memberAccount.push(workMemberList[i].memberAccount),
              (this.oldMemberName = this.oldMemberName
                ? this.oldMemberName + "," + workMemberList[i].memberName
                : workMemberList[i].memberName);
            this.oldMemberAccount = this.oldMemberAccount
              ? this.oldMemberAccount + "," + workMemberList[i].memberAccount
              : workMemberList[i].memberAccount;
          }
          break;
        case "部分安全措施变动":
          break;
      }
    },
    // 处理工作班成员
    getOptionData(nameArr, idArr, orgArr, orgName, orgId) {
      let option = [];
      nameArr.map((i, index) => {
        let item = {
          label: i,
          value: `${idArr[index]}${orgId[index]}`,
          id: idArr[index],
          isOutOrg: orgArr[index],
          orgName: orgName[index],
          orgId: orgId[index],
        };
        option.push(item);
      });
      return option;
    },
    // 提交
    doSubmit() {
      this.showDialogSubmit = true;
    },
    // 填写提交弹框确认
    async dialogSubmit() {
      let data = await this.$refs.basicForm.getFormData();
      delete data.workTicketType$;
      let otherData = {};
      let actionApi = null;
      switch (this.formData.type$ || data.type$) {
        case "工作负责人变更":
          otherData = await this.$refs.changeForm1.getFormData();
          actionApi = (params) => {
            return insertManagerData(params);
          };
          break;
        case "工作票延期":
          otherData = await this.$refs.changeForm2.getFormData();
          delete otherData.estimatedEndTime;
          actionApi = (params) => {
            return insertDelayData(params);
          };
          break;
        case "工作间断":
          otherData = await this.$refs.changeForm3.getFormData();
          delete otherData.createBy;
          delete otherData.createByName;
          delete otherData.deptId;
          delete otherData.deptName;
          delete otherData.description;
          delete otherData.updateBy;
          delete otherData.updateByName;
          delete otherData.updateTime;
          if (otherData.permitNameStart) {
            actionApi = (params) => {
              return updateIntermittentData(params);
            };
          } else {
            actionApi = (params) => {
              return insertIntermittentData(params);
            };
          }
          break;
        case "增加工作内容":
          otherData = await this.$refs.changeForm4.getFormData();
          actionApi = (params) => {
            return insertContentData(params);
          };
          break;
        case "工作班成员变动":
          otherData = await this.$refs.changeForm5.getFormData();
          actionApi = (params) => {
            return insertMemberData(params);
          };
          otherData.det = this.det;
          delete otherData.estimatedEndTime;
          delete otherData.memberNameArr;
          delete otherData.memberAccount;
          delete otherData.memberName;
          delete otherData.undefined;
          break;
        case "部分安全措施变动":
          otherData = await this.$refs.changeForm6.getFormData();
          actionApi = (params) => {
            return insertSafeData(params);
          };
          break;
      }
      data.updateTime = this.updateTime;
      delete data.type$;
      if (otherData.permitNameStart) {
        // 修改变更其他信息
        await actionApi(otherData).then((res) => {
          if (res.code == 200) {
            this.$message.success(res.description);
            this.showDialogSubmit = false;
            this.updateListData();
            location.reload();
          }
        });
      } else {
        // 变更基本信息
        await insertData(data).then(async (res) => {
          if (res.code == 200) {
            let changeId = {
              changeId: res.data,
              workTicketId: data.workTicketId,
            };
            otherData = Object.assign({}, otherData, changeId);
            // 变更其他信息
            await actionApi(otherData).then((res) => {
              if (res.code == 200) {
                this.$message.success(res.description);
                this.showDialogSubmit = false;
                this.updateListData();
                if (this.pageType != "details") {
                  let obj = {
                    formData: JSON.stringify({
                      id: otherData.changeId,
                      type: this.basicFormData.type,
                    }),
                    pageType: "details",
                  };
                  this.$router.push({
                    query: obj,
                  });
                }
                this.getDetails(otherData.changeId);
              }
            });
          }
        });
      }
    },
    // 点击选择工作票票号
    getTicketNum() {
      this.showDialogWork = true;
    },
    // 选中工作票票号确定按钮
    dialogSubmitWork() {
      let selectData = this.$refs.workOrderTable.getSelectData();
      if (!selectData) {
        this.$message.warning("请选择一行数据！");
      } else {
        let time = this.$utils.getCurrentDate("minutes");
        this.$set(this.changeFormData5, "changeTime", time);
        this.$set(this.changeFormData6, "changeTime", time);
        this.changeShow = true;
        this.showDialogWork = false;
        this.basicFormData = Object.assign(
          {},
          {
            type: selectData.type,
            workTicketCode: selectData.code,
            workTicketId: selectData.id,
          },
          this.basicFormData
        );
        let types = this.$store.getters["dirtData"]("ticket_work_type");
        let obj = types.find((i) => i.value == selectData.type);
        let type$ = obj ? obj.label : selectData.type;
        this.$set(this.basicFormData, "workTicketType", selectData.type);
        this.$set(this.basicFormData, "workTicketType$", type$);
        this.getInfo(selectData.id, selectData.type, 0);
      }
    },
    // 账号密码签名
    doSign(data, currentItem) {
      let formData = currentItem.formData;
      if (currentItem.disAccount == data.account) {
        if (formData == "changeForm1") {
          this.$message.error("工作许可人和新工作负责人不能为同一人");
        } else {
          this.$message.error("工作负责人和工作许可人不能为同一人");
        }
      } else if (
        data.account != currentItem.mustAccount &&
        currentItem.mustAccount
      ) {
        this.$message.error("请使用该工作票最新工作负责人的账号进行签名");
      } else {
        this.$refs[formData].setFormItem(currentItem.name, data.name);
        this.$refs[formData].setFormItem(currentItem.account, data.account);
        this.$refs[formData].getFormData();
      }
    },
    //获取详情信息
    async getDetails(id) {
      let val = { id };
      let { data } = await getById(val);
      this.changeId = data.code || "变更票号";
      this.creatInfo = data;
      if (data.type == "workInterrupt") {
        let val = { id: data.workTicketId, type: data.workTicketType };
        getWorkTicketData(val).then((res) => {
          if (res.code == 200) {
            if (res.data.ticketChange.ticketChangeDirectorList.length) {
              this.mustSignAccount =
                res.data.ticketChange.ticketChangeDirectorList[
                  res.data.ticketChange.ticketChangeDirectorList.length - 1
                ].newDirectorAccount;
            } else {
              this.mustSignAccount = res.data.workTicket.director;
            }
          }
        });
      }
      this.$set(this.basicFormData, "workTicketCode", data.workTicketCode);
      this.$set(this.basicFormData, "workTicketId", data.workTicketId);
      this.$set(this.basicFormData, "workTicketType", data.workTicketType);
      let types = this.$store.getters["dirtData"]("ticket_work_type");
      let obj = types.find((i) => i.value == data.workTicketType);
      let types0 = this.$store.getters["dirtData"]("ticket_change_type");
      let obj0 = types0.find((i) => i.value == data.type);
      let type = obj0.label || "";
      this.$set(this.basicFormData, "type$", type);
      this.$set(
        this.basicFormData,
        "workTicketType$",
        obj.label ? obj.label : data.workTicketType
      );
      switch (type) {
        case "工作负责人变更":
          this.changeFormData1 =
            data.ticketChangeDirectorList[
              data.ticketChangeDirectorList.length - 1
            ];
          break;
        case "工作票延期":
          this.changeFormData2 =
            data.ticketChangeDelayList[data.ticketChangeDelayList.length - 1];
          break;
        case "工作间断":
          this.changeFormData3 =
            data.ticketChangeInterruptList[
              data.ticketChangeInterruptList.length - 1
            ];
          if (this.pageType != "add") {
            if (this.changeFormData3.permitNameStart) {
              this.changeConfig3.itemArr.map((i) => (i.disabled = true));
              this.changeConfig3.itemArr.map((i) => (i.isHide = false));
            } else {
              this.$set(this.basicFormData, "workTicketId", data.workTicketId);
              this.$set(
                this.changeFormData3,
                "id",
                data.ticketChangeInterruptList[
                  data.ticketChangeInterruptList.length - 1
                ].id
              );
              this.changeConfig3.itemArr.map((i, index) =>
                index < 3 ? (i.disabled = true) : ""
              );
              this.changeConfig3.itemArr.map((i) => (i.isHide = false));
              this.rightBtnArr[0].hide = false;
            }
          }
          break;
        case "增加工作内容":
          this.changeFormData4 =
            data.ticketChangeContentList[
              data.ticketChangeContentList.length - 1
            ];
          break;
        case "工作班成员变动":
          this.changeConfig5.itemArr[1].isHide = true;
          this.changeFormData5 =
            data.ticketChangeMemberList[data.ticketChangeMemberList.length - 1];
          break;
        case "部分安全措施变动":
          this.changeFormData6 =
            data.ticketChangeSecurityArrangementList[
              data.ticketChangeSecurityArrangementList.length - 1
            ];
          break;
      }
    },
    // 跳转至工作票详情页
    goWorkTicket(ticketId, ticketType) {
      if (ticketId) {
        const { href } = this.$router.resolve({
          name: "workTicketProcessingDetails",
          query: {
            val: ticketId ? ticketId : "",
            pageType: "details",
            ticketType: ticketType,
          },
        });
        window.open(href, "_blank");
      }
    },
    /* 更新列表页的数据 */
    updateListData() {
      /* 通过localStorage来实现浏览器不同窗口之间的通信 */
      let num = Math.floor(Math.random() * 1000) + 1; //产生随机数，保证每次的值都不一样
      localStorage.setItem("update", num); //设置localStorage里面的值，保证每次都会触发storage事件
    },
  },
};
</script>
<style lang="less" scoped>
.container {
  height: 100%;
  width: 100%;
  // overflow: hidden;
  background-color: #ffffff;
  .title {
    font-size: 14px;
    font-weight: 500;
    color: #000;
    background: #f2f2f2;
    padding: 4px 10px;
    border-radius: 14px;
  }
  /deep/ .input-link .el-input__inner {
    color: #2c77f4;
    background-color: #f8f8f8;
  }
  /deep/ .input-link .el-input__inner:hover {
    cursor: pointer;
  }
  .TitleImg {
    margin-right: 2px;
    font-size: 22px;
  }
  .submitDialog {
    /deep/ .el-dialog {
      right: -660px;
    }
  }
  /deep/ .el-form-item__error {
    top: 90%;
  }
}
</style>